#include "../includes.h"


#define LMU_TOD_LENGTH	18

lmu_	lmu;

void Init_LMU()
{
	lmu.raw_count_all = 0;
	lmu.raw_digit_all = 0;
	lmu.prev_raw_digit_all = 0;

	lmu.STATUS 		= LMU_STATUS_NONE;

	lmu.enable_counter	= 0;
	lmu.disable_counter	= 0;
	lmu.skip_counter = 0;

	lmu.rxflag = OFF;
	lmu.RxCounter = 0;
	lmu.RxCheckCounter = 100;

	lmu.LockedCounter = 0;
	lmu.LOCKMODE = LMU_UNLOCKED;
}



void lmu_task()
{
	char	rxflag;
	int		RxCounter;
	int 	RxCheckCounter;

	lmu.RxCheckCounter++;

    lmu.raw_digit_all = ReadLMUCounter();

	lmu.raw_count_all = 10000000 - (lmu.raw_digit_all - lmu.prev_raw_digit_all);

	if(lmu.raw_count_all > 50000000) {lmu.raw_count_all = 100000000 - lmu.raw_count_all;}


	switch(lmu.STATUS){

		case LMU_STATUS_NONE:

				lmu.STATUS = LMU_STATUS_SET;
				lmu.enable_counter = 0;
				lmu.disable_counter = 0;

				lmu.RxCounter = 0;
				lmu.RxCheckCounter = 100;
				break;

		case LMU_STATUS_SET:

				//if((lmu.raw_count_all > -6 && lmu.raw_count_all < 6) && (lmu.raw_digit_all == lmu.raw_digit_all_2)){
				if((lmu.raw_count_all > -6 && lmu.raw_count_all < 6)){
					lmu.enable_counter++;
					lmu.disable_counter = 0;
				}
				else {
					lmu.enable_counter = 0;
					lmu.disable_counter++;
				}

				if( lmu.enable_counter > 10 ){


					if( lmu.RxCounter > 5 && lmu.RxCheckCounter < 5){

						lmu.STATUS = LMU_STATUS_RUN;
						lmu.disable_counter = 0;

						MessageOut((BYTE*)"LMU - in\r\n");
						LogIt(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_LMU_In,0);

						//CONTROL.SYNC = ON;
						
					}
				}

				if( lmu.disable_counter > 5 ){

					lmu.STATUS = LMU_STATUS_NONE;
				}
				break;

		case LMU_STATUS_RUN:
				if(! ((lmu.raw_count_all > -6 && lmu.raw_count_all < 6) )){
				//if(lmu.raw_count_all < -6 || lmu.raw_count_all > 6){
					lmu.disable_counter++;
				}
				else {
					lmu.disable_counter = 0;
				}

				if(lmu.skip_counter > 0 ){
					lmu.skip_counter--;
					lmu.disable_counter = 0;
				}

				if( lmu.disable_counter >= 2  || lmu.RxCheckCounter > 5 ){

					lmu.STATUS = LMU_STATUS_NONE;

					MessageOut((BYTE*)"LMU - out\r\n");
					LogIt(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_LMU_Out,0);
				}
				break;

	}


    if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG6){
       	sprintf((char*)MESG,"lmu:%d ro:%d rd:%d ec:%d dc:%d\r\n",lmu.STATUS,lmu.raw_count_all,lmu.raw_digit_all,lmu.enable_counter,lmu.disable_counter);
        TransmitMessage((BYTE*)MESG,DEBUG_PORT);
    }

	lmu.prev_raw_digit_all = lmu.raw_digit_all;
}




void julian_to_month_day(unsigned short year, unsigned short aday, unsigned char *m, unsigned char *d)
{

	int i;
	int day_sum;

	month[1] = 28;
	if (year%4 == 0)    month[1] = 29;
	if (year%100 == 0)  month[1] = 28;
	if (year%400 == 0)  month[1] = 29;

	day_sum = 0;
	for( i = 0; i < 12; i++ ){
		if( (day_sum + (int)month[i]) >= aday ){
			*m = i+1;
			*d = aday - day_sum;
			return;
		}
		day_sum += month[i];
	}
}

void DecodeLMUIn(BYTE *buffer,int *Counter)
{

	int i,j,index,aday;

	R_TOD_ * ptod_data;

	index = *Counter;

	for( i = (LMU_TOD_LENGTH-1); i < index; i++ ){

		ptod_data = (R_TOD_*)&buffer[i-(LMU_TOD_LENGTH-1)];

		//if(ptod_data->year10

		if( ptod_data->tail == 0x0D){

			lmu.rxflag = ON;
			lmu.RxCounter++;
			lmu.RxCheckCounter = 0;

			// time
			aday = ptod_data->day100*100 + ptod_data->day10*10 + ptod_data->day1;

			lmu.time.YEAR 	= (WORD)(ptod_data->year10*10 + ptod_data->year1 + 2000);
			julian_to_month_day(lmu.time.YEAR,(unsigned short)aday,&(lmu.time.MONTH),&(lmu.time.DAY));
			lmu.time.HOUR 	= ptod_data->hour10*10 + ptod_data->hour1;
			lmu.time.MINUTE 	= ptod_data->minute10*10 + ptod_data->minute1;
			lmu.time.SECOND 	= ptod_data->second10*10 + ptod_data->second1;

			if(((ptod_data->opmode&0x30) == 0x30) || ((ptod_data->opmode&0xC0) == 0xC0) ){
				lmu.LockedCounter++;
			}
			else {
				lmu.LockedCounter = 0;
			}

			if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG4){
				sprintf(MESG,"%4d %2d %2d %2d %2d %2d : ",
										lmu.time.YEAR,
										lmu.time.MONTH,
										lmu.time.DAY,
										lmu.time.HOUR,
										lmu.time.MINUTE,
										lmu.time.SECOND

										);

				TransmitMessage((unsigned char*)MESG,PORT_CONTROL);

				sprintf(MESG,"%2d> %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\r\n",
										i-17,

										ptod_data->year10,
										ptod_data->year1,
										ptod_data->day100,
										ptod_data->day10,
										ptod_data->day1,
										ptod_data->hour10,
										ptod_data->hour1,
										ptod_data->minute10,
										ptod_data->minute1,
										ptod_data->second10,
										ptod_data->second1,
										ptod_data->leapsecond10,
										ptod_data->leapsecond1,
										ptod_data->status,
										ptod_data->error,
										ptod_data->opmode,
										ptod_data->inform,
										ptod_data->tail

										);

				TransmitMessage((unsigned char*)MESG,PORT_CONTROL);
			}

			*Counter = 0;
			break;
		}
	}
}

